package pokeronlab_2014.mcts;

import com.biotools.meerkat.Deck;

/**
 * A játék állapotát tároló osztály
 * 
 * @author András
 *
 */
public class GameState {
	public int[] playerIDs;
	
	/**
	 * A korábbi körökben felgyűlt tétek 
	 */
	public int[] sumOfBets;
	/**
	 * Az aktuális kör tétjei. Vakok számában van megadva (nem fontosak a vakok).
	 */
	public int[] bets;
	public int[] board;
	public int currentSeat, button, stage, raiseNum, ourID;
	public Deck deck;
	
	private boolean[] toAct;
	private int betToCall; 
	
	/**
	 * Publikus konstruktor
	 */
	public GameState(){
		playerIDs = new int[10];
		toAct = new boolean[10];
		sumOfBets = new int[10];
		bets = new int[10];
		board = new int[5];
		deck = new Deck();
		deck.shuffle();
		stage = 3;
	}
	
	public GameState copy(){
		GameState ret = new GameState();
		ret.playerIDs = playerIDs.clone();
		ret.bets = bets.clone();
		ret.board = board.clone();
		ret.sumOfBets = sumOfBets.clone();
		ret.toAct = toAct.clone();
		ret.betToCall = betToCall;
		ret.button = button;
		ret.stage = stage;
		ret.raiseNum = raiseNum;
		ret.ourID = ourID;
		ret.deck = new Deck();
		ret.deck.copy(deck);
		return ret;
	}
	
	void nextPlayer() {
		if(!isEndOfTurn()){	
		currentSeat = getNextSeat();
		}
	}

	int getNextSeat(){
		int seat = currentSeat;
		do {
			seat++;
		//aki bedobta, illetve üres székek
		} while (playerIDs[seat % 10] <= 0);
		return seat % 10;
	}
	
	public int getCurrentPlayer() {
		return playerIDs[currentSeat];
	}

	public void fold() {
		toAct[currentSeat] = false;
		playerIDs[currentSeat] = -1;
		nextPlayer();
	}

	public void call() {
		if(betToCall > bets[currentSeat]){
			bets[currentSeat] = betToCall;
		}
		toAct[currentSeat] = false;
		nextPlayer();
	}

	public void raise() {
		//mindenkinek cselekednie kell
		for(int i = 0; i < 10; i++){
			toAct[i] = true;
		}
		raiseNum++;
		if(stage <= 3){
			betToCall++;
		}else{
			betToCall+=2;
		}
		//csak a megadandó tétet emeljük, azt majd megadja a call();
		//ugyanaz
		call();
	}

	void dealCard(int card){
		if(stage == 4){
			board[3] = card;
		}else if(stage == 5){
			board[4] = card;
		}
	}
	
	public boolean isEndOfTurn() {
		for(int i = 0; i < 10; i++){
			if(playerIDs[i] > 0 && toAct[i] == true)
				return false;
		}
		return true;
	}
	
	public void nextRound(int card) {
		stage++;
		dealCard(card);
		for(int i = 0; i< 10; i++){
			if(playerIDs[i] > 0)
				toAct[i] = true;
		}
		for(int i = 0; i< 10; i++){
			sumOfBets[i] += bets[i];
			bets[i] = 0;
		}
		betToCall = 0;
		raiseNum = 0;
		currentSeat = button;
		nextPlayer();
	}
	//TODO további feltételt
	public boolean isGameOver() {
		if(isEndOfTurn() && stage == 5){
			return true;
		}
		return false;
	}
	
	public boolean canRaise(){
		if(raiseNum >= 4){
			return false;
		}else{
			return true;
		}
	}
	
	public boolean isOurTurn(){
		if(playerIDs[currentSeat] == ourID){
			return true;
		} else{
			return false;
		}	
	}
}
